package com.vincent.service;

import com.vincent.common.enums.ResultMapKey;
import com.vincent.dao.AdminRoleDao;
import com.vincent.dao.RoleDao;
import com.vincent.dao.RolePermissionDao;
import com.vincent.model.Role;
import com.vincent.vo.RoleAddVo;
import com.vincent.vo.RoleEditVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by vincent on 15-1-3.
 */
@Service("roleService")
public class RoleService {
    @Resource
    private RoleDao roleDao;
    @Resource
    private RolePermissionDao rolePermissionDao;
    @Resource
    private AdminRoleDao adminRoleDao;

    public Map<String,Object> getRoleManage(){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        List<Role> roles = roleDao.selectAll();
        Map<String,Object> datamap=new HashMap<String, Object>();
        datamap.put("total",roles.size());
        datamap.put("rows",roles);
        resultMap.put(ResultMapKey.SUCCESS.getCode(),datamap);
        return resultMap;
    }


    public Map<String,Object> saveRole(RoleAddVo vo){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        Role role = roleDao.selectByName(vo.getName());
        if(role!=null){
            resultMap.put(ResultMapKey.ERROR.getCode(),"角色名字已存在");
            return resultMap;
        }
        role=new Role();
        role.setName(vo.getName());
        role.setDescription(vo.getDesc());
        roleDao.saveRole(role);
        resultMap.put(ResultMapKey.SUCCESS.getCode(),"保存角色成功");
        return resultMap;
    }
    public Map<String,Object> updateRole(RoleEditVo vo){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        Role selectById = roleDao.selectById(vo.getId());
        if(selectById==null){
            resultMap.put(ResultMapKey.ERROR.getCode(),"角色不存在");
            return resultMap;
        }
        Role selectByName = roleDao.selectByName(vo.getName());
        if(selectByName!=null&&!selectById.getName().equals(vo.getName())){
            resultMap.put(ResultMapKey.ERROR.getCode(),"角色名存在");
            return resultMap;
        }
        Role role = new Role();
        role.setId(vo.getId());
        role.setDescription(vo.getDesc());
        role.setName(vo.getName());
        roleDao.updateRole(role);
        resultMap.put(ResultMapKey.SUCCESS.getCode(),"编辑角色成功");
        return resultMap;
    }

    public Map<String,Object> deleteRole(Integer id){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        Integer[] ids = {id};
        adminRoleDao.deleteByRoleIds(ids);
        rolePermissionDao.deleteByRoleIds(ids);
        roleDao.deleteRole(id);
        resultMap.put(ResultMapKey.SUCCESS.getCode(),"删除角色成功");
        return resultMap;
    }

    public Map<String,Object> getRole(Integer id){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        Role role = roleDao.selectById(id);
        if(role==null){
            resultMap.put(ResultMapKey.ERROR.getCode(),"角色不存在");
            return resultMap;
        }
        resultMap.put(ResultMapKey.SUCCESS.getCode(),role);
        return resultMap;
    }



}
